<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if (isset($req->__password) && $req->__password != '') {
      $req->password = md5($req->salt . $req->__password);
      unset($req->__password);
    }
    
    $orm->update('pb_ecom_customer', $req);
    
    if (isset($req->__pg)) {
      $req->__objs = $orm->get('pb_ecom_customer', 't.account', 'ASC', $req->__pg, SIZE, NULL);
    }
    echo json_encode($req);
  }
  
  public static function get_objs($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));

    $opts = new stdClass();
    if (isset($req->search->type) && isset($req->search->phrase)) {
      if ($req->search->type == 1) {
        $opts->cd = 't.status = :v1';
        $opts->cdv = array(':v1' => 0);
      } else if ($req->search->type == 2) {
        $opts->cd = 't.status = :v1';
        $opts->cdv = array(':v1' => 1);
      } else if ($req->search->type == 3) {
        $opts->cd = 't.status = :v1';
        $opts->cdv = array(':v1' => 2);
      }
      
      if ($req->search->type != 0) {
        $opts->cd .= ' AND ';
      }
      
      $opts->cd .= '(t.account LIKE :v3 OR t.first_name LIKE :v3 OR t.last_name LIKE :v3 OR t.phone LIKE :v3)';
      $opts->cdv[":v3"] = '%' . $req->search->phrase . '%';
    }
    $res = $orm->get('pb_ecom_customer', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($res);
  }
}
?>